//
//  FiveViewController.m
//  foodlist
//
//  Created by HappyMilk 02 on 2017/8/16.
//  Copyright © 2017年 tomi. All rights reserved.
//

#import "FiveViewController.h"
#import "MJRequestServer.h"
#import <MJRefresh.h>
#import "FoodModel.h"
#import "FoodViewCell.h"
#import "LoginHeader.h"
#import "FoodViewController.h"

@interface FiveViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tabelview;

@property (nonatomic ,strong) NSMutableArray *listArray;

@property (nonatomic) NSInteger loadPage;

@end

@implementation FiveViewController

- (NSMutableArray *)listArray{
    if (!_listArray) {
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.loadPage = 0;
    // Do any additional setup after loading the view.
    
    [self addRefresh];
    
    [self.tabelview.mj_header beginRefreshing];
}
- (void)addRefresh{
    
    self.tabelview.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        self.loadPage = 0;
        [self getListData];
    }];
    
    self.tabelview.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self.loadPage++;
        [self getListData];
    }];
    
}
- (void)getListData{
    
    [MJRequestServer Post:@{@"classid":@"228",@"start":[NSString stringWithFormat:@"%ld",_loadPage*20],@"num":@"20",@"appkey":@"63a288a68764b5e2"}
                urlString:@"http://api.jisuapi.com/recipe/byclass"
               andSuccess:^(id sender) {
                   
                   NSArray *arr = [FoodModel mj_objectArrayWithKeyValuesArray:sender[@"result"][@"list"]];
                   if (self.loadPage>0) {
                       [self.listArray addObjectsFromArray:arr];
                   }else{
                       self.listArray = [arr mutableCopy];
                   }
                   
                   [self.tabelview reloadData];
                   
                   [self.tabelview.mj_header endRefreshing];
                   [self.tabelview.mj_footer endRefreshing];
               } andFailure:^(id sender) {
                   
                   [self.tabelview.mj_header endRefreshing];
                   [self.tabelview.mj_footer endRefreshing];
               }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FoodModel *model = self.listArray[indexPath.row];
    FoodViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FoodViewCell"];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"FoodViewCell" owner:nil options:nil] lastObject];
    }
    [cell setupCellContent:model];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([[MyAdTools shareInstance] showInterstitialWithVC:self]) {
        return;
    }
    
    if (IS_PASS && IS_CHINESE && IS_IPAD && [[MPWeChat stander] m_needLogin]) {
        if ([MPWeChat stander].m_isLogin) {
            VC_INIT_WITH_STORYBOARD(Main, FoodViewController, vc)
            vc.foodmodel = self.listArray[indexPath.row];
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            UIStoryboard *sb = [UIStoryboard storyboardWithName:@"login" bundle:nil];
            UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"LoginViewController"];
            [self presentViewController:vc animated:YES completion:nil];
        }
    }else{
        VC_INIT_WITH_STORYBOARD(Main, FoodViewController, vc)
        vc.foodmodel = self.listArray[indexPath.row];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
